package com.xique.park.controller.client;

import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.web.controller.BaseController;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.core.web.page.TableDataInfo;
import com.xique.common.log.annotation.Log;
import com.xique.common.log.enums.BusinessType;
import com.xique.common.security.annotation.PreAuthorize;
import com.xique.park.bean.domain.ParkLane;
import com.xique.park.bean.domain.client.ClientLaneConfig;
import com.xique.park.bean.domain.client.ParkClient;
import com.xique.park.bean.request.client.OpenGateRequest;
import com.xique.park.bean.request.client.ParkClientSelectLaneRequest;
import com.xique.park.bean.vo.client.OpenGateVo;
import com.xique.park.service.service.IClientLaneConfigService;
import com.xique.park.service.service.IParkClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 岗亭管理Controller
 *
 * @author ww
 * @date 2019-10-15
 */
@RestController
@RequestMapping("/parkclient")
public class ParkClientController extends BaseController {

    @Autowired
    private IParkClientService parkClientService;

    @Autowired
    private IClientLaneConfigService clientLaneConfigService;

    /**
     * 查询岗亭管理列表
     */
    @PreAuthorize(hasPermi = "park:client:list")
    @GetMapping("/list")
    public TableDataInfo list(ParkClient parkClient) {
        startPage();
        parkClient.setParkId(SecurityUtils.getParkId());
        List<ParkClient> list = parkClientService.selectParkClientList(parkClient);
        return getDataTable(list);
    }

//    /**
//     * 导出岗亭管理列表
//     */
//    @PreAuthorize(hasPermi = "oms:parkclient:export")
//    @PostMapping("/export")
//    @ResponseBody
//    public AjaxResult export(ParkClient parkClient)
//    {
//        List<ParkClient> list = parkClientService.selectParkClientList(parkClient);
//        ExcelUtil<ParkClient> util = new ExcelUtil<ParkClient>(ParkClient.class);
//        return util.exportExcel(list, "parkclient");
//    }

    /**
     * 新增保存岗亭管理
     */
    @PreAuthorize(hasPermi = "park:client:add")
    @Log(title = "岗亭管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult addSave(@RequestBody ParkClient parkClient) {
//        ParkClient search = new ParkClient();
//        search.setCode(parkClient.getCode());
//        List<ParkClient> parkClients = parkClientService.selectParkClientList(search);
//        if (!parkClients.isEmpty()){
//            return AjaxResult.error("序列号重复，请重新输入！");
//        }
        parkClient.setParkId(SecurityUtils.getParkId());
        return toAjax(parkClientService.insertParkClient(parkClient));
    }

    /**
     * 修改保存岗亭管理
     */
    @PreAuthorize(hasPermi = "park:client:edit")
    @Log(title = "岗亭管理", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult editSave(@RequestBody ParkClient parkClient) {
        return toAjax(parkClientService.updateParkClient(parkClient));
    }

    /**
     * 删除岗亭管理
     */
    @PreAuthorize(hasPermi = "park:client:remove")
    @Log(title = "岗亭管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove")
    public AjaxResult remove(@RequestParam("ids") String ids) {
        return toAjax(parkClientService.deleteParkClientByIds(ids));
    }

//    /**
//     * 绑定通道页面跳转
//     */
//    @PreAuthorize(hasPermi = "oms:parkclient:edit")
//    @GetMapping("/bandLane/{id}")
//    public String bandLane(@PathVariable("id") Long id, ModelMap mmap)
//    {
//        mmap.put("client", parkClientService.selectParkClientById(id));
//        return prefix + "/addLane";
//    }

    /**
     * 查询已分配通道列表
     */
    @PreAuthorize(hasPermi = "park:client:list")
    @GetMapping("/bandLane/allocatedList")
    public TableDataInfo allocatedList(ParkClientSelectLaneRequest request) {
        startPage();
        //查询当前岗亭的通道列表
        request.setParkId(String.valueOf(SecurityUtils.getParkId()));
        List<ParkLane> list = parkClientService.selectClientLaneAllocatedList(request);
        return getDataTable(list);
    }

    /**
     * 查询未分配设备权限组列表
     */
    @PreAuthorize(hasPermi = "park:client:list")
    @GetMapping("/bandLane/unallocatedList")
    public TableDataInfo unallocatedList(ParkClientSelectLaneRequest request) {
        startPage();
        request.setParkId(String.valueOf(SecurityUtils.getParkId()));
        List<ParkLane> list = parkClientService.selectClientLaneUnAllocatedList(request);
        return getDataTable(list);
    }

    /**
     * 批量绑定通道
     */
    @PreAuthorize(hasPermi = "park:client:laneAdd")
    @Log(title = "批量绑定通道", businessType = BusinessType.GRANT)
    @PostMapping("/bandLane/addAll")
    public AjaxResult addClientLaneAll(@RequestBody ParkClientSelectLaneRequest request) {
        return parkClientService.addClientLaneAll(request);
    }

    /**
     * 单个取消授权
     */
    @PreAuthorize(hasPermi = "park:client:laneCanecl")
    @Log(title = "单个取消授权", businessType = BusinessType.GRANT)
    @PostMapping("/bandLane/cancel")
    public AjaxResult cancelLane(@RequestBody ParkClientSelectLaneRequest request) {
        return parkClientService.cancelLane(request);
    }

    /**
     * 批量取消授权
     */
    @Log(title = "批量取消授权", businessType = BusinessType.GRANT)
    @PostMapping("/bandLane/cancelAll")
    public AjaxResult cancelAll(@RequestBody ParkClientSelectLaneRequest request) {
        return parkClientService.cancelLane(request);
    }

//    @GetMapping("/bandLane/updateSort")
//    public String updateSort(@RequestParam(value = "clientId") String clientId, @RequestParam(value = "laneId") String laneId, ModelMap mmap)
//    {
//        mmap.put("clientId",clientId);
//        mmap.put("laneId", laneId);
//        ClientLaneConfig search = new ClientLaneConfig();
//        search.setClientId(Long.valueOf(clientId));
//        search.setLaneId(Long.valueOf(laneId));
//        List<ClientLaneConfig> clientLaneConfigs = clientLaneConfigService.selectClientLaneConfigList(search);
//        Integer sort = 0;
//        if (!clientLaneConfigs.isEmpty()){
//            sort =  clientLaneConfigs.get(0).getSort();
//        }
//        mmap.put("sort", sort);
//        return prefix + "/editSort";
//    }

    @PostMapping("/updateClientLaneConfig")
    public AjaxResult updateClientLaneConfig(@RequestBody ClientLaneConfig config) {
        Integer sort = config.getSort();
        List<ClientLaneConfig> clientLaneConfigs = clientLaneConfigService.selectClientLaneConfigList(config);
        ClientLaneConfig clientLaneConfig = clientLaneConfigs.get(0);
        clientLaneConfig.setSort(sort);
        return toAjax(clientLaneConfigService.updateClientLaneConfig(clientLaneConfig));
    }

    @PreAuthorize(hasPermi = "parking:thegate:list")
    @GetMapping("/record/opengate")
    public TableDataInfo openGateRecords(OpenGateRequest request) {
        startPage();
        request.setParkId(SecurityUtils.getParkId());
        List<OpenGateVo> list = parkClientService.selectOpenGateRecords(request);
        return getDataTable(list);
    }

}
